/* Copyright (c) 2010, mitsuhisa.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of the misuhisa.net nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <vector>
#include <cppunit/extensions/HelperMacros.h>
#include "../libnyu/Hash.hpp"

using namespace nyu;

class HashTest : public CPPUNIT_NS::TestFixture {
    CPPUNIT_TEST_SUITE(HashTest);
    CPPUNIT_TEST(testPutGet);
    CPPUNIT_TEST(testSize);
    CPPUNIT_TEST(testEachKeys);
    CPPUNIT_TEST_SUITE_END();

    struct HashFunc {
        Hash<int,int>::code_type operator()(int k) const {
            return Hash<int,int>::code_type(k);
        }

        bool operator()(int k1, int k2) const {
            return k1 == k2;
        }
    };

    struct EachKeysFunc {
        Hash<int,int>& h_;
        int n_;

        EachKeysFunc(Hash<int,int>& h) : h_(h), n_(0) {
        }

        void operator()(int k) {
            CPPUNIT_ASSERT_EQUAL(k, n_);

            int nn = 0;
            CPPUNIT_ASSERT(h_.get(k, nn, HashFunc()));

            switch (n_++) {
            case 0:
                CPPUNIT_ASSERT_EQUAL(nn, 100);
                break;
            case 1:
                CPPUNIT_ASSERT_EQUAL(nn, 200);
                break;
            case 2:
                CPPUNIT_ASSERT_EQUAL(nn, 300);
                break;
            }
        }
    };

public:
    void testPutGet() {
        Hash<int,int> h;
        HashFunc fn;

        h.put(0, 100, fn);
        h.put(1, 200, fn);
        h.put(2, 300, fn);

        int n = 0;
        CPPUNIT_ASSERT(h.get(0, n, fn));
        CPPUNIT_ASSERT_EQUAL(n, 100);
        CPPUNIT_ASSERT(h.get(1, n, fn));
        CPPUNIT_ASSERT_EQUAL(n, 200);
        CPPUNIT_ASSERT(h.get(2, n, fn));
        CPPUNIT_ASSERT_EQUAL(n, 300);
        CPPUNIT_ASSERT(!h.get(3, n, fn));
    }

    void testSize() {
        Hash<int,int> h;
        HashFunc fn;

        h.put(0, 100, fn);
        h.put(1, 200, fn);
        h.put(2, 300, fn);

        CPPUNIT_ASSERT_EQUAL(h.size(), size_t(3));
    }

    void testEachKeys() {
        Hash<int,int> h;
        HashFunc fn;

        h.put(0, 100, fn);
        h.put(1, 200, fn);
        h.put(2, 300, fn);

        h.eachKeys(EachKeysFunc(h));
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(HashTest);
